package com.seeyon.apps.kk.form.workflowevent;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.form.bean.FormBean;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
import com.seeyon.kk.utils.CAP4FormKitUtils;

/**
 *
 * 审批控件 按选择顺序 分配 审批 协议人员
 * 
 * @author Administrator
 *
 */
public class MemberShenPiFenPeiExtendPlug extends KkBaseExtendPlug {

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "kkMemberShenPiFenPeiExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "审批协议人员赋值节点事件";
	}

	/**
	 * 返回指定的模版编号
	 * 
	 * @return
	 */
	public String getTemplateCode() {
		return "";
	};

	// 发起前事件
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		Map<String, Object> formdata = data.getBusinessData();
		FormBean formBean = (FormBean) formdata.get("formBean");
		List<String> sp=Lists.newArrayList();
		List<String> xy=Lists.newArrayList();
		
		for (int i = 1; i <=10; i++) {
			String member=MapUtils.getString(formdata, "审批"+i);
			String stype=MapUtils.getString(formdata, "类型"+i);
			if(StringUtils.isNotEmpty(member)){
				if(StringUtils.isEmpty(stype)){
					WorkflowEventResult er=new WorkflowEventResult();
					er.setAlertMessage("请为已选审批人员设置审批类型！");
					return er;
				}else{
					if(StringUtils.equals("-5475001439774683176", stype)){					 
						sp.add(member);
					}else{
						xy.add(member);
					} 
				}
			}
			
		}
		FormDataMasterBean master = (FormDataMasterBean) formdata.get("formDataBean");
		for (int i = 1; i <=5; i++) {
			String mv="";
			if(sp.size()>=i){
				mv=sp.get(i-1);
			}
			
			CAP4FormKitUtils.setCellValue(master, "审批-姓名"+i, mv);
		}

		for (int i = 1; i <=5; i++) {
			String mv="";
			if(xy.size()>=i){
				mv=xy.get(i-1);
			}
			
			CAP4FormKitUtils.setCellValue(master, "审批-姓名"+i, mv);
		}
		 
		
		return null;
	}

	// 发起事件
	public void onStart(WorkflowEventData data) {
		Map<String, Object> formdata = data.getBusinessData();
		FormBean formBean = (FormBean) formdata.get("formBean");
		ColSummary summary = (ColSummary) data.getSummaryObj();

	}

	@Override
	public WorkflowEventResult onBeforeProcessFinished(WorkflowEventData data) {
		 //匹配审批协议的人员并设置意见 与 处理时间
		
		
		return null;
	}
	
	
	

}
